import { defineStore } from 'pinia'

export const useVenueStore = defineStore('venue', {
  state: () => ({
    // 场地类型列表
    venueTypes: [],
    // 场地列表
    venues: [],
    // 当前选中的场地类型ID
    currentVenueTypeId: null
  }),
  
  getters: {
    // 获取指定类型的场地
    getVenuesByType: (state) => {
      return (typeId) => state.venues.filter(venue => venue.venueTypeId === typeId)
    },
    // 获取场地类型名称
    getVenueTypeName: (state) => {
      return (typeId) => {
        const type = state.venueTypes.find(type => type.id === typeId)
        return type ? type.name : ''
      }
    }
  },
  
  actions: {
    // 设置场地类型列表
    setVenueTypes(types) {
      this.venueTypes = types
    },
    
    // 设置场地列表
    setVenues(venues) {
      this.venues = venues
    },
    
    // 设置当前选中的场地类型
    setCurrentVenueTypeId(id) {
      this.currentVenueTypeId = id
    },
    
    // 添加场地类型
    addVenueType(type) {
      this.venueTypes.push(type)
    },
    
    // 更新场地类型
    updateVenueType(updatedType) {
      const index = this.venueTypes.findIndex(type => type.id === updatedType.id)
      if (index !== -1) {
        this.venueTypes[index] = updatedType
      }
    },
    
    // 删除场地类型
    deleteVenueType(typeId) {
      this.venueTypes = this.venueTypes.filter(type => type.id !== typeId)
    },
    
    // 添加场地
    addVenue(venue) {
      this.venues.push(venue)
    },
    
    // 删除场地
    deleteVenue(venueId) {
      this.venues = this.venues.filter(venue => venue.id !== venueId)
    }
  }
})